/**
 * 
 */
package de.oszimtcc.googleapi;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.google.api.services.calendar.model.Event;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ListView;
import de.oszimtcc.framework.BaseActivity;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.TimetableApplication;


/**
 * @author reuter
 *
 */
public class AppointmentListView extends ListView
{

		private BaseActivity activity;
		private AppointmentReader appointmentConnection;
		private AppointmentAdapter adapter;
		private Timer updateAppointments;
		

		public AppointmentListView(Context context)
	    {
		    super(context);
		    iniListView();
	    }
		
		public AppointmentListView(Context context, AttributeSet attrs, int defStyle)
	    {
		    super(context, attrs, defStyle);
		    iniListView();
	    }


		public AppointmentListView(Context context, AttributeSet attrs)
	    {
		    super(context, attrs);
		    iniListView();
	    }
		
		private void iniListView()
		{
	    	this.activity = TimetableApplication.getInstance().getActivity();
	    	appointmentConnection = new AppointmentReader(this);
	    	LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(activity, R.anim.layoutanimation);
	    	setLayoutAnimation(controller);
		}
		
		public void addListEntry(Event pToAdd)
		{
			if(adapter == null)
			{
				List<Event> toAdd = new ArrayList<Event>();
				toAdd.add(pToAdd);
				updateAppointments = new Timer();
				updateAppointments.schedule((new TimerTask() 
				{
					
					@Override
					public void run()
					{
						appointmentConnection.onAuthToken();
					}
				}), 10000, 10000);
				adapter = new AppointmentAdapter(activity, toAdd);
		    	this.setAdapter(adapter);
			}
			this.adapter.addData(pToAdd);
		}
}
